package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/role")
public class RoleController {

    private final RoleService roleService;

    @PostMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult<PageResponse<Role>> findPage(@RequestBody RoleDto roleDto, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
        PageResponse<Role> page = roleService.findPage(roleDto,pageNum,pageSize);
        return ResponseResult.success(page);
    }

    @PutMapping
    public ResponseResult insertRole(@RequestBody RoleDto roleDto){
        Boolean flag = roleService.insertRole(roleDto);
        return ResponseResult.success(flag);
    }

    @PatchMapping
    public ResponseResult updateRole(@RequestBody RoleDto roleDto){
        Boolean flag = roleService.updateRole(roleDto);
        return ResponseResult.success(flag);
    }

    @GetMapping("/find-checked-resources/{roleId}")
    public ResponseResult getResourcesById(@PathVariable Long roleId){
        Set<String> set = roleService.getResourcesById(roleId);
        return ResponseResult.success(set);
    }

    @DeleteMapping("/{roleId}")
    public ResponseResult deleteByRoleId(@PathVariable Long roleId){
        roleService.deleteByRoleId(roleId);
        return ResponseResult.success();
    }

    /**
     * 获取角色下拉框,用于服务用户增加时的角色选择
     * @return
     */
    @PostMapping("/init-roles")
    public ResponseResult getDetails(){
        List<RoleVo> list = roleService.initRoles();
        return ResponseResult.success(list);
    }


}
